///////////////////////////////////////////////////////////////////////////////
// \author (c) DaLinCaiNiao
//             2022, Shanghai, China
//
// \license The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// \brief STM32 UART related functions.
//
// \version
// v0.0.1: 2022.03.20, Initial version.
///////////////////////////////////////////////////////////////////////////////

#ifndef UART_H_
#define UART_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx_hal.h"

#define UART_RECORD_LOST_NUM					(1)
#define UART_BUF_SIZE_IS_2POW					(1)
#define USE_MY_PRINTF							(1)
#if USE_MY_PRINTF
#include "printf.h"
#endif

#define UART1_TX_BUF_SIZE						(1024)
#define UART1_RX_BUF_SIZE						(1024)

#define UART1_TX_SIZE_NOT_2POW					((UART1_TX_BUF_SIZE - 1) & UART1_TX_BUF_SIZE)
#define UART1_RX_SIZE_NOT_2POW					((UART1_RX_BUF_SIZE - 1) & UART1_RX_BUF_SIZE)
#if (UART_BUF_SIZE_IS_2POW && (UART1_TX_SIZE_NOT_2POW || UART1_RX_SIZE_NOT_2POW))
#error "Uart1 buffer size is not power of two."
#endif

typedef struct _UartBuf_t
{
#if UART_RECORD_LOST_NUM
    uint32_t TxLostNum;
#endif
    uint8_t* TxBuf;
#if UART_BUF_SIZE_IS_2POW
    uint16_t TxIn;
    uint16_t TxOut;
    uint16_t TxSize;
#else
    int16_t TxIn;
    int16_t TxOut;
    int16_t TxSize;
#endif

#if UART_RECORD_LOST_NUM
    uint32_t RxLostNum;
#endif
    uint8_t* RxBuf;
#if UART_BUF_SIZE_IS_2POW
    uint16_t RxIn;
    uint16_t RxOut;
    uint16_t RxSize;
#else
    int16_t RxIn;
    int16_t RxOut;
    int16_t RxSize;
#endif
}UartBuf_t;

extern UART_HandleTypeDef Uart1Handle;

extern uint8_t Uart1TxBuf[UART1_TX_BUF_SIZE];
extern uint8_t Uart1RxBuf[UART1_RX_BUF_SIZE];
extern UartBuf_t Uart1Buf;

#define UART_HANDLE					Uart1Handle
#define UART_BUF					Uart1Buf

HAL_StatusTypeDef InitUart1(void);
void CustomUartPrintf(char* fmt, ...);
void UartIrqService(USART_TypeDef* UartX, UartBuf_t* Buf);

#ifdef __cplusplus
}
#endif

#endif /* UART_H_ */
